package com.gun3y.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.gun3y.entity.media.Movie;
import com.gun3y.entity.media.PropertyType;
import com.gun3y.entity.pattern.ExpressionType;
import com.gun3y.entity.pattern.FieldType;
import com.gun3y.exception.ExceptionCode;
import com.gun3y.exception.SWException;
import com.gun3y.manager.MovieGraphManager;
import com.gun3y.manager.WrapperGraphManager;
import com.gun3y.utils.Commons;

@Controller
@RequestMapping("/views/")
public class ViewController {

    // @Autowired
    // private WrapperResourceManager wrapperResourceManager;

    @Autowired
    private WrapperGraphManager wrapperGraphManager;

    @Autowired
    private MovieGraphManager movieGraphManager;

    @RequestMapping(value = "/create-wrapper", method = RequestMethod.GET)
    public String handleCreateWrapper(@RequestParam(value = "id", required = false) String id, ModelMap model) {

	if (StringUtils.isNotBlank(id)) {
	    if (!wrapperGraphManager.exists(id)) {
		throw new SWException(ExceptionCode.NoWrapper);
	    }

	    model.addAttribute(Commons.CONS_WRAPPER, wrapperGraphManager.getWrapperByName(id));
	}

	return "createWrapper";
    }

    @RequestMapping(value = "/create-wrapper-extract", method = RequestMethod.GET)
    public ModelAndView handleCreateWrapperExtract(@RequestParam("name") String name, @RequestParam("description") String description,
	    @RequestParam("encoding") String encoding, @RequestParam("baseUrl") String baseUrl, @RequestParam("testUrl") String testUrl) {

	ModelAndView view = new ModelAndView("createWrapperExtract");
	view.addObject("fieldTypeList", FieldType.values());
	view.addObject("functionTypeList", ExpressionType.getFunctions());
	view.addObject("filterTypeList", ExpressionType.getFilters());
	view.addObject("fieldNames", PropertyType.values());

	view.addObject("name", name);
	view.addObject("description", description);
	view.addObject("encoding", encoding);
	view.addObject("baseUrl", baseUrl);
	view.addObject("testUrl", testUrl);
	view.addObject("edit", false);
	if (wrapperGraphManager.exists(name)) {
	    view.addObject("edit", true);
	    view.addObject(Commons.CONS_WRAPPER, wrapperGraphManager.getWrapperByName(name));
	}

	return view;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String handleIndex(ModelMap model) {

	model.addAttribute("wrappers", wrapperGraphManager.getAllWrappers());

	return "index";
    }

    @RequestMapping(value = "/graph", method = RequestMethod.GET)
    public String handleMovieGraph(ModelMap model) {
	return "graph";
    }

    @RequestMapping(value = "/movie", method = RequestMethod.GET)
    public String handleMovie(@RequestParam(value = "id", required = false) String id, ModelMap model) {

	List<Movie> movies = movieGraphManager.getMovies();

	model.addAttribute("movies", movies);

	if (StringUtils.isNotBlank(id)) {
	    model.addAttribute("movie", movieGraphManager.getMovieByName(id));
	}
	else if (!movies.isEmpty()) {
	    model.addAttribute("movie", movies.get(0));
	}
	return "movie";
    }

    @ExceptionHandler(SWException.class)
    public String handleIOException(SWException e, ModelMap modelMap) {

	return "error";
    }

    @ExceptionHandler(Exception.class)
    public String handleIOException(Exception e, ModelMap modelMap) {

	return "error";
    }

}
